﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：优惠劵管理客户端脚本
//创建人：赵国伟
//生成时间：2012-04-15 00:13:11
//----------------------------------------------------------------*/
var AjaxServerUrl = "/Manage/AS/CouponsCommon.aspx";
var ModelOfCoupon;
var ModelOfUserCoupon;
var CouponID = 0;
var StartDateOfCoupon;
var EndDateOfCoupon;
var SelectOptionOfUseStatus = "";
var ctrlOfSelectUsers;

///初始化
function Init() {
    CouponID = 0;
}

function GoToSearch() {
    var url = "CouponsManage.aspx?";
    url += "keyWords=" + escape($("#txtKeyWords").val().trim());
    url += "&UseStatus=" + escape($("#ddlUseStatusOfSearch").val());
    url += "&AllotStatus=" + escape($("#ddlAllotStatusOfSearch").val());
    url += "&ExpiredStatus=" + escape($("#ddlExpiredStatusOfSearch").val());
    url += "&GenerateMethod=" + escape($("#ddlGenerateMethodOfSearch").val());
    location.href = url;
}

//保存设置的分配信息；
function SaveAssign() {
    $("#btnSave").attr("disabled", true);
    if (ctrlOfSelectUsers.SelectValue() == "") {
        alert("提示：请选择会员！");
        $("#btnSelUsers").focus();
        $("#btnSave").attr("disabled", false);
        return false;
    }
    var data = clone(ModelOfUserCoupon);
    data._couponid = CouponID;
    data._gaindate = "/Date(" + CurrentDateStringToUTC() + ")/";
    data._gainmethod = "商城赠送";
    data._userid = ctrlOfSelectUsers.SelectValue();
    data._memo = $("#txtMemo").val();
    var url = AjaxServerUrl + "?Action=SaveAssign";
    AjaxPost(url, "btnSave", data);  //更新
}

///分配优惠劵给指定的会员；
function Assign(id) {
    Init();
    CouponID = id;
    var url = AjaxServerUrl + "?action=GetCoupon&CouponID=" + CouponID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                GetPopupDiv(600, 800, "#e2e2e2", "优惠劵分配", GetAssignHtml(data));
                ctrlOfSelectUsers = new SelectUsers("btnSelUsers", "Single", "spSelUsers", "");
                if (data.Owner != null) {
                    var d = new Array();
                    d.push(data.Owner);
                    ctrlOfSelectUsers.SetDefaultValue(d);
                }
            }
            else {
                alert("系统提示：未找到对应的优惠劵信息！");
            }
        }
    });

   
}

function GetAssignHtml(data) {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"600\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"2\" class=\"TableHeadRow\">&gt;&gt;优惠劵分配</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"90\" align=\"right\">优惠劵编号：</td>");
    html.push("    <td class=\"blue bold\">" + data.Coupon._couponid + "</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">优惠劵金额：</td>");
    html.push("    <td class=\"blue bold\">¥" + data.Coupon._couponmoney + "</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\" >有效期：</td>");
    html.push("    <td class=\"blue bold\">" + ConvertJsonDatetoToLocateDate(data.Coupon._startdate) + "  至 " + ConvertJsonDatetoToLocateDate(data.Coupon._enddate) + "</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">分配会员：</td>");
    html.push("    <td><span class=\"red\">");
    html.push("      <input name=\"btnSelUsers\" type=\"button\" id=\"btnSelUsers\" value=\"请选择会员..\" class=\"btn2\"/>");
    html.push("      (*)</span> &nbsp;&nbsp;<span id=\"spSelUsers\"></span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">分配理由：</td>");
    html.push("    <td><span class=\"red\">");
    html.push("    <textarea name=\"txtMemo\" cols=\"60\" rows=\"3\" class=\"border6\" id=\"txtMemo\"></textarea>");
    html.push("      (*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"SaveAssign();\">");
    html.push("      <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"关闭本窗口\" class=\"btn2\" onclick=\"yesOther(false);\"></td>");
    html.push("  </tr>");
    html.push("</table>");
    html.push("</form>");
    return html.join("");
}

//执行删除
function Delete(id) {
    if (!confirm("系统提示：您确定要删除该条优惠劵信息？"))
        return;
    AjaxGet(AjaxServerUrl + "?Action=DeleteCoupon&CouponID=" + id);
}

///检查表单；
function CheckForm() {
    if ($("#txtStartDate").val() == "") {
        alert("系统提示：请输入优惠劵开始日期！");
        return false;
    }
    if ($("#txtEndDate").val() == "") {
        alert("系统提示：请输入优惠劵结束日期！");
       
        return false;
    }
    if (parseInt($("#txtStartDate").val().replace(/-/g, "")) > parseInt($("#txtEndDate").val().replace(/-/g, ""))) {
        alert("系统提示：开始日期不能大于结束日期！");
        return false;
    }
    return true;
}

//保存；
function Save() {
    $("#btnSave").attr("disabled", true);
    if (!CheckForm()) {
        $("#btnSave").attr("disabled", false);
        return;
    }

    var data = clone(ModelOfCoupon);
    data._couponid = CouponID;
    data._startdate = "/Date(" + DateStringToUTC($("#txtStartDate").val()) + ")/";
    data._enddate = "/Date(" + DateStringToUTC($("#txtEndDate").val()) + ")/";
    data._couponmoney = $("#ddlCouponMoney").val();
    data._generatemethod = "手动";
    data._usestatus = $("#ddlStatus").val();
    data._memo = $("#txtMemo").val();
    data._createdate = "/Date(" + CurrentDateStringToUTC() + ")/";

    var url = AjaxServerUrl + "?Action=SaveCoupon";
    if (CouponID == 0)
        url += "&Quantity=" + $("#ddlQuantity").val();
    AjaxPost(url, "btnSave", data);  //更新
}

//绑定优惠劵信息到录入表单中；
function BindCouponToForm() {
    var url = AjaxServerUrl + "?action=GetCouponInfo&CouponID=" + CouponID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                $("#txtStartDate").val(ConvertJsonDatetoToLocateDate(data._startdate));
                $("#txtEndDate").val(ConvertJsonDatetoToLocateDate(data._enddate));
                $("#ddlCouponMoney").val(data._couponmoney);
                $("#ddlStatus").val(data._usestatus);
                $("#txtMemo").val(data._memo);
            }
        }
    });
}

///添加界面；
function Add() {
    Init();
    GetPopupDiv(700, 800, "#e2e2e2", "生成优惠劵", GetHtmlForm());
}

//编辑界面；
function Edit(id) {
    Init();
    CouponID = id;
    GetPopupDiv(600, 800, "#e2e2e2", "编辑优惠劵", GetHtmlForm());
    BindCouponToForm();
}

///获取录入界面的HTML;
function GetHtmlForm() {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"600\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"2\" class=\"TableHeadRow\">&gt;&gt;优惠劵信息</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"97\" align=\"right\">使用日期：</td>");
    html.push("    <td width=\"503\"><input name=\"txtStartDate\" type=\"text\" value=\"" + StartDateOfCoupon + "\"   id=\"txtStartDate\"  size=\"12\" readonly=\"readonly\" onfocus=\"new WdatePicker({el:this})\" class=\" Wdate\" style=\"cursor: hand\">");
    html.push("      -");
    html.push("        <input name=\"txtEndDate\" type=\"text\" value=\"" + EndDateOfCoupon + "\"   id=\"txtEndDate\" size=\"12\" readonly=\"readonly\" onfocus=\"new WdatePicker({el:this})\" class=\" Wdate\" style=\"cursor: hand\"/>");
    html.push("    <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">面额：</td>");
    html.push("    <td><select name=\"ddlCouponMoney\" id=\"ddlCouponMoney\">");
    html.push("      <option value=\"10\" selected=\"selected\">10元</option>");
    html.push("      <option value=\"20\">20元</option>");
    html.push("      <option value=\"30\">30元</option>");
    html.push("      <option value=\"50\">50元</option>");
    html.push("      <option value=\"100\">100元</option>");
    html.push("            </select></td>");
    html.push("  </tr>");

    if (CouponID == 0) {
        html.push("  <tr>");
        html.push("    <td align=\"right\">数量：</td>");
        html.push("    <td><select name=\"ddlQuantity\" id=\"ddlQuantity\">");
        for (var i = 1; i <= 50; ++i)
            html.push("      <option value=\"" + i + "\">" + i + "张</option>");

        html.push("        </select>");
        html.push("      (一次最多50张)</td>");
        html.push("  </tr>");
    }
    html.push("  <tr>");
    html.push("    <td align=\"right\">状态：</td>");
    html.push("    <td><select name=\"ddlStatus\" id=\"ddlStatus\">");
    html.push(SelectOptionOfUseStatus);
    html.push("    </select>    </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">备注：</td>");
    html.push("    <td><textarea name=\"txtMemo\" cols=\"50\" rows=\"3\" class=\"border6\" id=\"txtMemo\"></textarea></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td>");
    if (CouponID == 0)
        html.push("<input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 生 成 \" class=\"btn2\" onclick=\"Save();\">");
    else
        html.push("<input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"Save();\">");
    html.push("    <input name=\"btnReset\" type=\"reset\" id=\"btnReset\" value=\" 重 置 \" class=\"btn2\">");
    html.push("    <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"关闭本窗口\" class=\"btn2\" onclick=\"yesOther(false);\"></td>");
    html.push("  </tr>");
    html.push("</table>");
    html.push("</form>");

    return html.join("");

}













